package com.example.jsf.controller;

import com.example.jsf.Repository.IncomeRepository;
import com.example.jsf.entity.Income;
import com.example.jsf.entity.IncomeVo;
import com.example.jsf.other.DataHelper;
import com.example.jsf.other.Result;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.annotation.Transient;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.transaction.Transactional;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("income")
public class IncomeController {

    @Autowired
    private IncomeRepository incomeRepository;

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @GetMapping("getTest")
    public Result test(){
//        List<Income> query = jdbcTemplate.query("select * from income", new BeanPropertyRowMapper<Income>(Income.class));
//        String sql = "UPDATE income i JOIN goods g ON g.id = 33 SET g.goods_num = 5";
        String sql = "UPDATE income SET goods_price = ? WHERE id = ?";
        int sql1 = jdbcTemplate.update("UPDATE income SET goods_price = ? WHERE id = ?", new Object[]{100, 1});
        return  Result.success(sql1);
    }

    @PostMapping("addIncome")
    @Transactional(rollbackOn = Exception.class)
    public Result addIncome(@RequestBody IncomeVo incomeVo) {
        System.out.println(incomeVo);
        Income income = new Income();

        if(incomeVo.getGoodsId() == null){
            BeanUtils.copyProperties(incomeVo,income);
            income.setCreateTime(new Date());
            return  Result.success(incomeRepository.save(income));
        }else{
            BeanUtils.copyProperties(incomeVo,income);
            income.setCreateTime(new Date());
            Income save = incomeRepository.save(income);
            if(incomeVo.getGoodsNum() != null && incomeVo.getGoodsId() != null){
                int update = jdbcTemplate.update("UPDATE goods SET goods_num = ? WHERE id = ?", new Object[]{incomeVo.getGoodsNum(), incomeVo.getGoodsId()});
                return Result.success(update);
            }
            return Result.success("");
        }


    }

    @GetMapping("getIncome")
    public Result getIncome(
            @RequestParam("pageNo") int pageNo,
            @RequestParam(value = "pageSize",defaultValue = "2") int pageSize
            ,String goodsName,
            String startTime,
            String stopTime,
            int addressType
    ) {
        String s = DataHelper.selectSQL(Income.class);
        if(StringUtils.hasText(goodsName)){
            s+="where goods_name = '"+ goodsName+"' ";
        }
        if(StringUtils.hasText(startTime) && StringUtils.hasText(stopTime)){
            s+="where create_time BETWEEN '"+ startTime+"' and '"+ stopTime+"' and  address_type = '"+ addressType+"' AND  ISNULL(is_test) " ;
        }else{
            s+="where address_type = '"+ addressType+"'  AND  ISNULL(is_test) ";
        }


        s+=" order by create_time desc";

//        System.out.println("s = " + s);
        return Result.success(DataHelper.pageQuery(jdbcTemplate,s,pageNo,pageSize,Income.class));

    }
}
